package com.atguigu.eduservice.controller;


import com.atguigu.annotation.WebLog;
import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.springframework.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author 陈烨庆
 * @since 2021-12-20
 */
@Api("讲师控制器")
@RestController
@RequestMapping("/eduservice/edu-teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    /**
     * 1、查询讲师表所有数据
     * rest 风格
     */
    @WebLog("讲师列表")
    @ApiOperation("讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher() {

        // 调用 Service 方法查询所有教师
        List<EduTeacher> teacherList = teacherService.list(null);
        return R.ok().data("items", teacherList);
    }

    /**
     * 逻辑删除讲师的方法
     */
    @WebLog("逻辑删除")
    @ApiOperation("逻辑删除")
    @DeleteMapping("removeTeacherById/{id}")
    public R removeTeacherById(@PathVariable String id) {
        boolean b = teacherService.removeById(id);
        return R.ok();
    }

    /**
     * 分页查询讲师列表
     */
    @WebLog("分页查询讲师列表")
    @ApiOperation("分页查询讲师列表")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,
                             @PathVariable long limit) {
        // page对象
        Page<EduTeacher> eduTeacherPage = new Page<>(current, limit);

        // 调用方法分页
        IPage<EduTeacher> page = teacherService.page(eduTeacherPage, null);
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();

        return R.ok().data("total", total).data("records", records);
    }

    /**
     * 分页查询讲师列表
     */
    @WebLog("分页多条件查询讲师列表")
    @ApiOperation("分页多条件查询讲师列表")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageListTeacherCondition(@PathVariable long current,
                                      @PathVariable long limit,
                                      @RequestBody(required = false) TeacherQuery teacherQuery) {
        // page对象
        Page<EduTeacher> eduTeacherPage = new Page<>(current, limit);

        // wrapper对象
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        // 多条件组合查询 动态sql
        wrapper.like(!ObjectUtils.isEmpty(teacherQuery.getName()), "name", teacherQuery.getName());
        wrapper.eq(!ObjectUtils.isEmpty(teacherQuery.getLevel()), "level", teacherQuery.getLevel());
        wrapper.ge(!ObjectUtils.isEmpty(teacherQuery.getBegin()), "gmt_create", teacherQuery.getBegin());
        wrapper.le(!ObjectUtils.isEmpty(teacherQuery.getEnd()), "gmt_create", teacherQuery.getEnd());
        wrapper.orderByDesc("gmt_create");

        // 调用方法分页
        IPage<EduTeacher> page = teacherService.page(eduTeacherPage, wrapper);
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();

        return R.ok().data("total", total).data("records", records);
    }

    /**
     * 添加讲师
     * 1. 自动填充
     * @param eduTeacher
     * @return
     */
    @ApiOperation("添加讲师")
    @WebLog("添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = teacherService.save(eduTeacher);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 根据ID查询讲师 用于更新回显
     * @param id
     * @return
     */
    @ApiOperation("根据ID查询讲师")
    @WebLog("根据ID查询讲师")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id) {
        EduTeacher eduTeacher = teacherService.getById(id);
        return R.ok().data("teacher", eduTeacher);
    }

    /**
     * 修改讲师
     * @param eduTeacher
     * @return
     */
    @ApiOperation("根据id更新讲师")
    @WebLog("根据id更新讲师")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = teacherService.updateById(eduTeacher);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }


}

